/**
 * UserController
 *
 * @description :: Server-side logic for managing users
 * @help        :: See http://sailsjs.org/#!/documentation/concepts/Controllers
 */

module.exports = {
		
	list : function(req, res) {
		
		var contacts = [];
		var userid = req.session.user.userid;
		
		User
		.findOne(userid)
		.then(function findOneCB(current){
			
			var currentDepts = current.department;
			
			Department
			.find({'id' : currentDepts})
			.populate('userlist')
			.then(function findOneCB(contacts){
				return res.view('mobile/contacts/contacts-list', {
					contacts : contacts,
				});
			});
			
		}).catch(function(err){
			sails.log.error(err);
			return res.serverError(err);
		});
	},
	
	detail : function(req, res){
		
		var userid = req.param('userid');
		
		User
		.findOne({userid:userid})
		.populate('deptlist')
		.then(function findOneCB(result){
			
			return res.view('mobile/contacts/contacts-detail', {
				contact : result,
			});
		}).catch(function(err){
			sails.log.error(err);
			return res.serverError(err);
		});
		
	}
};

